package org.example.netty.Protobuf.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.example.netty.Protobuf.MyDataInfo;

public class protobufClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
     // 当连接建立后，向服务器发送数据
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        DataInfo.Student student = DataInfo.Student.newBuilder().setName("张三").setAge(20).setAddress("aaa").build();
        MyDataInfo.Person p = MyDataInfo.Person.newBuilder()
                .setName("靶机")
                .setAge(20)
                .setAddress("枪型").build();

        MyDataInfo.MyMessage mm = MyDataInfo.MyMessage.newBuilder()
                .setDataType(MyDataInfo.MyMessage.DataType.PersonType)
                .setPerson(p).build();
        ctx.writeAndFlush(mm);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {

    }
}
